﻿using AddModule9.ServiceReference1;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data.Services.Client;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AddModule9.Views
{
    /// <summary>
    /// Interaction logic for AddModule9MainView.xaml
    /// </summary>
    /// 

     [Export("AddModule9MainView")]
    public partial class AddModule9MainView : UserControl
    {

        ServiceReference1.Container context 
            = new ServiceReference1.Container(new Uri("http://localhost:11245/odata/"));
        public AddModule9MainView()
        {
            InitializeComponent();

            Combo();

           

          
        }

         public void Combo()
        {
            var pp = context.products.Select(kkk => kkk);


            //Lot_ProductCombo.ItemsSource = pp;
            //Lot_ProductCombo.DisplayMemberPath = "work_order_number";
            //Lot_ProductCombo.DisplayMemberPath = "productplan";

            foreach (var kk in pp)
            {
                MoveCombo.Items.Add(kk.product_name);
            }

            var con = context.containers.Select(kkk => kkk);

            foreach (var kkk in con)
            {
                ContainerCombo1.Items.Add(kkk.container_name);
                ContainerCombo2.Items.Add(kkk.container_name);
            }

            ContainerCombo1.SelectionChanged += (sender, e) =>
            {
                var con2 = context.containers.Where(kkk => ContainerCombo1.SelectedItem.ToString() == kkk.container_name).SingleOrDefault();

                this.txtContainer1.Text = con2.container_decription;
            };

            ContainerCombo2.SelectionChanged += (sender, e) =>
            {
                var con3 = context.containers.Where(kkk => ContainerCombo2.SelectedItem.ToString() == kkk.container_name).SingleOrDefault();

                this.txtContainer2.Text = con3.container_decription;
            };






        }

        private void btnMove_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var movecount = context.product_move.ToList().Max(kkk => kkk.Id);

                product_move insertMove = new product_move
                {
                    Id = movecount +1,
                    product_container = this.ContainerCombo1.SelectedItem.ToString(),
                    product_move_container = this.ContainerCombo2.SelectedItem.ToString(),
                    product_move_date = this.DateMove.SelectedDate.Value,
                    product_move_type = 1,
                    product_name = this.MoveCombo.SelectedItem.ToString(),
                    product_quantity = Convert.ToInt32( this.txtQuan.Text)
                    };

                    //context.AddObject("Location_table", insertLocation);
                    context.AddToproduct_move(insertMove);

                    //context.BeginSaveChanges(AsynchCallback, insertLocation);
                    context.SaveChanges();

                  

                    MessageBox.Show(this.MoveCombo.SelectedItem.ToString() + "의 이동처리가 완료 되었습니다.", "이동처리완료 메세지");

                   // this.ContainerCombo1.SelectedIndex = -1;
                   // this.ContainerCombo2.SelectedIndex = -1;
                   //this.MoveCombo.SelectedIndex = -1;
                   this.txtContainer1.Text = "";
                   this.txtContainer2.Text = "";
                   this.txtQuan.Text = "";

                    //location_tableDataGrid.DataContext = context.Location_table;

            }
            catch (DataServiceRequestException dataServiceRequestException)
            {
                MessageBox.Show("같은 값이 있네요~");
            }
            catch (FormatException formatException)
            {
                MessageBox.Show("값을 입력하시오");
            }

        }
    }
}
